package com.bank.controller;

import com.bank.pojo.Transaction;
import com.bank.pojo.User;
import com.bank.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

@RestController
@RequestMapping("/api/user")
public class UserController {
    private final UserService userService;

    @Autowired
    public UserController(UserService userService) {
        this.userService = userService;
    }

    // 存款
    @PostMapping("/deposit")
    public ResponseEntity<?> deposit(@RequestBody Transaction request) {
        try {
            userService.deposit(request.getNumber(), request.getAmount());
            return ResponseEntity.ok(Map.of("success", true, "message", "存款成功"));
        } catch (Exception e) {
            return ResponseEntity.badRequest().body(Map.of("success", false, "message", e.getMessage()));
        }
    }

    // 取款
    @PostMapping("/withdraw")
    public ResponseEntity<?> withdraw(@RequestBody Transaction request) {
        try {
            userService.withdraw(request.getNumber(), request.getAmount());
            return ResponseEntity.ok(Map.of("success", true, "message", "取款成功"));
        } catch (Exception e) {
            return ResponseEntity.badRequest().body(Map.of("success", false, "message", e.getMessage()));
        }
    }

    // 获取用户信息
    @GetMapping("/{number}")
    public ResponseEntity<User> getUser(@PathVariable String number) {
        User user = userService.findByUserNumber(number);
        return ResponseEntity.ok(user);
    }
}